package handlers

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"user-perm-mgr/modles"
)

func RAdd(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	uid, _ := strconv.Atoi(c.PostForm("uid"))
	data := modles.UserRole{
		Uid: rid,
		Rid: uid,
	}
	modles.DB.Create(&data)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "关联成功",
	})
}

func RList(c *gin.Context) {
	uid := c.Query("uid")
	var data []modles.Role
	/*
		SELECT
		  *
		FROM
		  roles
		  JOIN user_roles ON roles.id = user_roles.rid
		WHERE
		  user_roles.uid = 1
	*/
	modles.DB.Table("roles").
		Joins(" JOIN user_roles ON roles.id = user_roles.rid").
		Where("user_roles.uid = ?", uid).Find(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查找成功",
		"data": data,
	})
}
